<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Order
 * @package App
 */
class Order extends Model
{
    public $timestamps = false;
    protected $table = 's_order';


    const BODY = '康侣支付';

    const NOTIFY_URL = 'https://wechat.jkbanlv.com/check';

    //状态
    const STATUS_ON = 1;
    const STATUS_PAYING = 2;
    const STATUS_RECEIVING = 3;
    const STATUS_PING = 4;

    static $StatusDisplay = [
        self::STATUS_ON => '进行中',
        self::STATUS_PAYING => '待付款',
        self::STATUS_PING => '待评价',
        self::STATUS_RECEIVING => '待收货',
    ];


    static $win = [2, 4];

    /**
     * 统一下单
     * @param $app
     * @param $out_trade_no
     * @param $total_fee
     * @param $openid
     * @return array
     */
    static function UnifiedOrder($app, $out_trade_no, $total_fee, $openid)
    {
        $result = $app->order->unify([
            'body' => self::BODY,
            'out_trade_no' => $out_trade_no,
            'total_fee' => $total_fee,
            'trade_type' => 'JSAPI',
            'openid' => $openid,
            'spbill_create_ip' => $_SERVER['SERVER_ADDR'],
            'notify_url' => self::NOTIFY_URL
        ]);
        //数据是否发送成功
        if ($result['return_code'] !== 'SUCCESS' || !isset($result['result_code'])) {
            logger(['out_trade_no' => $out_trade_no, 'result' => $result]);
            return ['code' => 500, 'msg' => '支付失败'];
        }
        //订单是否成功
        if ($result['result_code'] !== 'SUCCESS' || !isset($result['prepay_id'])) {
            logger(['out_trade_no' => $out_trade_no, 'result' => $result]);
            return ['code' => 500, 'msg' => '支付失败'];
        }
        $json = $app->jssdk->sdkConfig($result['prepay_id'], false);
        return ['code' => 200, 'msg' => $json, 'config' => $result];
    }


    static function PaySuccess($order_id)
    {

    }

}
